﻿using HAActiveMQ.Entity;
using HAActiveMQ.Interface;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace HAActiveMQ
{
    class Program : IMqReceiveMsg
    {
        static string url = "activemq:failover:(tcp://172.16.98.133:61618,tcp://172.16.98.133:61619,tcp://172.16.98.133:61620)?randomize=false&priorityBackup=true";
        static MqProducer producer = new MqProducer(url);
        static MqConsumer<Program> consumer = new MqConsumer<Program>(url);
        static Program program=new Program();
        static void Main(string[] args)
        {
            //设置消费者对象
            consumer.OnMessage(program, program.GetType().GetMethod("ReceiveMessage"));
            //添加两个消费者用于处理高并发 正常情况下只添加一个就可以了
            consumer.AddQueueListener("ha_message");
            //consumer.AddQueueListener("ha_message");
            DateTime date= DateTime.Now;
            Task task = testhamq();
            task.Wait();
            Console.WriteLine(date - DateTime.Now);
            Console.ReadKey();
        }

        public static async Task testhamq()
        {
            string input = "";
            Console.WriteLine("请输入你要打印的内容。");
            int i = 0;
            List<Task> taskList = new List<Task>();
            while (i < 50000)
            {
                input = "" + i;
                Task task = producer.ProducerQueue(input, "ha_message");
                taskList.Add(task);
                await Task.Delay(10);
                i++;
            }
            await Task.WhenAll(taskList.ToArray());
        }

        public void ReceiveMessage(string message)
        {
            Console.Write($"{DateTime.Now.ToString("HH:mm:ss fff")}:你输入的是“{message}”。");
        }
    }
}
